﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BTGISWASECO.BusinessEntities;
using System.Data;

/// <summary>
/// Summary description for OngNhanhDAL
/// </summary>
/// 
namespace BTGISWASECO.DataLayer
{
    public class OngNhanhDAL
    {
        DataAccessHelper db = new DataAccessHelper();
        static System.Data.DataTable resultsDataTable;
        static ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer rs;
        public OngNhanhDAL(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer)
        {
            resultsDataTable = resultsGraphicsLayer;
        }

        public OngNhanhDAL()
        {
            
      
        }
        public OngNhanhEntities GetOngNhanhEntitiesByID(int id)
        {
            OngNhanhEntities OngNhanh = new OngNhanhEntities();

            List<OngNhanhEntities> list = GetList();
            foreach (OngNhanhEntities b in list)
                if (b.OBJECTID == id)
                {
                    OngNhanh = b;
                    break;
                }

            return OngNhanh;
        }

        public OngNhanhEntities GetOngNhanhEntitiesByID()
        {
            OngNhanhEntities OngNhanh = new OngNhanhEntities();

            List<OngNhanhEntities> list = GetList();
            foreach (OngNhanhEntities b in list)
            {
                OngNhanh = b;
                break;
            }

            return OngNhanh;
        }

        public static List<OngNhanhEntities> GetList()
        {

            DataAccessHelper db = new DataAccessHelper();
            DataTable dt = db.GetDateTable();

            List<OngNhanhEntities> list = new List<OngNhanhEntities>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new OngNhanhEntities(row));
            }

            return list;
        }


        public static List<OngNhanhEntities> GetListByName(string keyword)
        {
            DataTable dt = resultsDataTable;

            List<OngNhanhEntities> list = new List<OngNhanhEntities>();

            foreach (DataRow row in dt.Rows)
            {
                list.Add(new OngNhanhEntities(row));
            }
            return list;
        }

        //public bool Insert(OngNhanhEntities DongHoKhachHang)
        //{
        //   // int count = db.ExecuteNonQuery("INSERT INTO OngNhanhEntitiess VALUES(N'" + DongHoKhachHang.OngNhanhEntitiesName + "',N'" + DongHoKhachHang.Author + "'," + DongHoKhachHang.Amount + "," + DongHoKhachHang.Price + ")");
        //   // return count > 0;
        //    re
        //}

        public void Update(int obj, string IDMaDuongOng, string IDDMA, int HuongDongChay, double ChieuDai)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wOngNhanh")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == "BTGISWASECO.SDE.wOngNhanh")
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;
                workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
                workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();
                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                queryFilter.WhereClause = "OBJECTID =" + obj;
                queryFilter.SubFields = "*";
                ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
                int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
                int IDDMAIndex = featureClass.FindField("IDDMA");
                int HuongDongChayIndex = updateCursor.FindField("HuongDongChay");
                int ChieuDaiIndex = featureClass.FindField("ChieuDai");
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    while ((feature = updateCursor.NextFeature()) != null)
                    {
                        feature.set_Value(IDMaDuongOngIndex, (object)IDMaDuongOng);
                        feature.set_Value(IDDMAIndex, (object)IDDMA);
                        feature.set_Value(HuongDongChayIndex, HuongDongChay);
                        feature.set_Value(ChieuDaiIndex, ChieuDai);
                        updateCursor.UpdateFeature(feature);
                    }
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }

        //public bool Delete(OngNhanhEntities DongHoKhachHang)
        //{
        //    int count = db.ExecuteNonQuery("DELETE OngNhanhEntitiess WHERE OngNhanhEntitiesID=" + DongHoKhachHang.OngNhanhEntitiesID);
        //    return count > 0;
        //}

        #region vi du
        public void Update1(OngNhanhEntities OngNhanhEntities)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
                // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
                // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wOngNhanh")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == "BTGISWASECO.SDE.wOngNhanh")
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;
                workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
                workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();
                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                queryFilter.WhereClause = "OBJECTID =" + OngNhanhEntities.OBJECTID;
                queryFilter.SubFields = "*";
                ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
                //int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
                //int IDDMAIndex = featureClass.FindField("IDDMA");
                //int HuongDongChayIndex = featureClass.FindField("HuongDongChay");
                //int ChieuDaiIndex = featureClass.FindField("ChieuDai");
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    while ((feature = updateCursor.NextFeature()) != null)
                    {
                        //feature.set_Value(featureClass.FindField("IDMaDuongOng"), (object)OngNhanhEntities.IDMaDoanOng);
                        //feature.set_Value(featureClass.FindField("IDDMA"), (object)OngNhanhEntities.IDDMA);
                        //feature.set_Value(featureClass.FindField("HuongDongChay"), OngNhanhEntities.HuongDongChay);
                        //feature.set_Value(featureClass.FindField("ChieuDai"), OngNhanhEntities.ChieuDai);

                        feature.set_Value(featureClass.FindField("IDDoanOng"), (object)OngNhanhEntities.IDDoanOng);
                        feature.set_Value(featureClass.FindField("IDDMA"), (object)OngNhanhEntities.IDDMA);
                        feature.set_Value(featureClass.FindField("IDDMZ"), (object)OngNhanhEntities.IDDMZ);
                        feature.set_Value(featureClass.FindField("HuongDongChay"), OngNhanhEntities.HuongDongChay);
                        feature.set_Value(featureClass.FindField("ChieuDai"), OngNhanhEntities.ChieuDai);
                        feature.set_Value(featureClass.FindField("DKTrong"), (object)OngNhanhEntities.DKTrong);
                        feature.set_Value(featureClass.FindField("DKNgoai"), (object)OngNhanhEntities.DKNgoai);
                        feature.set_Value(featureClass.FindField("CoOng"), OngNhanhEntities.CoOng);
                        feature.set_Value(featureClass.FindField("VatLieu"), OngNhanhEntities.VatLieu);
                        feature.set_Value(featureClass.FindField("TieuChuanVL"), (object)OngNhanhEntities.TieuChuanVL);
                        feature.set_Value(featureClass.FindField("NhaSanXuat"), (object)OngNhanhEntities.NhaSanXuat);
                        feature.set_Value(featureClass.FindField("NuocSanXuat"), OngNhanhEntities.NuocSanXuat);
                        feature.set_Value(featureClass.FindField("NamSanXuat"), OngNhanhEntities.NamSanXuat);
                        feature.set_Value(featureClass.FindField("NamLapDat"), (object)OngNhanhEntities.NamLapDat);
                        feature.set_Value(featureClass.FindField("DoNhamDanhDinh"), (object)OngNhanhEntities.DoNhamDanhDinh);
                        feature.set_Value(featureClass.FindField("DoNhamThucTe"), OngNhanhEntities.DoNhamThucTe);
                        feature.set_Value(featureClass.FindField("ALHoatDong"), OngNhanhEntities.ALHoatDong);
                        feature.set_Value(featureClass.FindField("ApLucThietKe"), (object)OngNhanhEntities.ApLucThietKe);
                        feature.set_Value(featureClass.FindField("DiemDau"), (object)OngNhanhEntities.DiemDau);
                        feature.set_Value(featureClass.FindField("DiemCuoi"), OngNhanhEntities.DiemCuoi);
                        feature.set_Value(featureClass.FindField("DoSau"), OngNhanhEntities.DoSau);
                        feature.set_Value(featureClass.FindField("DoDoc"), (object)OngNhanhEntities.DoDoc);
                        feature.set_Value(featureClass.FindField("TinhTrang"), (object)OngNhanhEntities.TinhTrang);
                        feature.set_Value(featureClass.FindField("SoLanSuCo"), OngNhanhEntities.SoLanSuCo);
                        feature.set_Value(featureClass.FindField("DonViTK"), OngNhanhEntities.DonViTK);
                        feature.set_Value(featureClass.FindField("DonViTC"), (object)OngNhanhEntities.DonViTC);
                        feature.set_Value(featureClass.FindField("DonViGS"), (object)OngNhanhEntities.DonViGS);
                        feature.set_Value(featureClass.FindField("NguoiCNCuoi"), OngNhanhEntities.NguoiCNCuoi);
                        feature.set_Value(featureClass.FindField("NgayCNCuoi"), OngNhanhEntities.NgayCNCuoi);
                        feature.set_Value(featureClass.FindField("GhiChu"), (object)OngNhanhEntities.GhiChu);
                        feature.set_Value(featureClass.FindField("IDHinhAnh"), (object)OngNhanhEntities.IDHinhAnh);
                        updateCursor.UpdateFeature(feature);
                    }
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }
        #endregion vi du

        public static DataTable GetDateTable(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer)
        {
            System.Data.DataTable resultsDataTable = resultsGraphicsLayer;
            return resultsDataTable;
        }
    }
}